package org.example.ecr;

import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ecr.EcrClient;
import software.amazon.awssdk.services.ecr.model.DescribeRepositoriesRequest;
import software.amazon.awssdk.services.ecr.model.DescribeRepositoriesResponse;
import software.amazon.awssdk.services.ecr.model.Repository;


/**
 * Hello world!
 *
 */
public class AwsEcrProfile1 {

    public static void main( String[] args ) {
        // Replace these values with your AWS credentials and ECR repository details
        //String region = "your-region";
        String region = "cn-north-1";
        //String repositoryName = "your-repository-name";


        // Create an ECR client
        EcrClient ecrClient = EcrClient.builder()
                .region(Region.of(region))
                .credentialsProvider(ProfileCredentialsProvider.builder().profileName("jack").build()).build();


        /*DescribeRegistryRequest registryRequest = DescribeRegistryRequest.builder().build();
        DescribeRegistryResponse registryResponse = ecrClient.describeRegistry(registryRequest);
        System.out.println(registryResponse.registryId());
        System.out.println(registryResponse);*/

        // 查询仓库
        DescribeRepositoriesRequest describeRepositoriesRequest = DescribeRepositoriesRequest.builder().build();
        DescribeRepositoriesResponse describeRepositoriesResponse = ecrClient.describeRepositories(describeRepositoriesRequest);

        // 打印仓库数量和信息
        System.out.println(describeRepositoriesResponse.toString());
        System.out.println("Number of repositories: " + describeRepositoriesResponse.repositories().size());
        System.out.println("Repository names:");
        for (Repository repository : describeRepositoriesResponse.repositories()) {
//            System.out.println(repository.repositoryName());
            System.out.println("repositoryUri:"+repository.repositoryUri());
            System.out.println("repositoryId:"+repository.registryId());
        }


        // Describe images in the repository
        /*String repositoryName = "cd-api-service";

        DescribeImagesRequest describeImagesRequest = DescribeImagesRequest.builder()
                .repositoryName(repositoryName)
                .build();

        DescribeImagesResponse describeImagesResponse = ecrClient.describeImages(describeImagesRequest);
        for (ImageDetail imageDetail : describeImagesResponse.imageDetails()) {
            System.out.println("Image Digest: " + imageDetail.imageDigest());
            System.out.println("Image Tags: " + imageDetail.imageTags());
            System.out.println("Image Pushed At: " + imageDetail.imagePushedAt());
            // Add more fields as needed
            System.out.println("----------------------------------------");
        }*/


        // Close the ECR client
        ecrClient.close();
    }

}


